﻿using MJTop.Model.SYS;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Collections.Specialized;

namespace MJTop.DAL.SYS
{
    public class UserDAL : MJTop.DAL.DAO.DAL
    {

        public SYS_USER Login(string loginName, string password)
        {
            return DbTop.Get<SYS_USER>("sys_user", new { loginname = loginName, passwd = password });
        }

        public bool ChangePassWd(Guid id, string password)
        {
            string updateSql = "update SYS_USER set passwd=@passwd where ID=@id ";

            return DbTop.ExecSql(updateSql, new { passwd = password, id = id.ToString() }) > 0;
        }

        public Pager GetPager(ReqParam req)
        {
            var kv = DbTop.GetDataTableByPager(req.CurrentPage, req.PageSize, "*", "SYS_USER (nolock)", req.Where, req.Orders);             
            Pager pager = new Pager()
            {
                list = kv.Key,
                pageCurrent = req.CurrentPage,
                pageSize = req.PageSize,
                totalRow = kv.Value
            };
            return pager;
        }

        public DataTable SelectTable(ReqParam req)
        {
            return DbTop.SelectTable("SYS_USER (nolock)", req.Where, req.Orders);
        }

        public bool Upsert(NameValueCollection nvc)
        {
            return DbTop.Save(nvc, "SYS_USER", "id").Value;
        }

        public SYS_USER GetUser(string id)
        {
            return DbTop.GetById<SYS_USER>("sys_user", id);
        }

        public int Delete(string id)
        {
            int res= DbTop.Delete("sys_user", id);
            return res;
        }
    }
}
